import { createRouter, createWebHashHistory } from 'vue-router'
import { constantRoute } from './routes'
import useSettingStore from '@/store/modules/setting'


const router = createRouter({
  history: createWebHashHistory(),
  routes: constantRoute,
  // scrollBehavior() {
  //   return {
  //     left: 0,
  //     top: 0
  //   }
  // }
})

// 白名单
const whiteList = ['/login', '/404']

router.beforeEach((to: any, from: any, next: any) => {
  const settings = useSettingStore()
  document.title = `${'paper-frog'} - ${to.meta.title}`
  const token = window.sessionStorage.getItem('token')
  if (to.meta?.showBackBtn) {
    settings.handlerGoBackShow(true) // 展示返回按钮
    settings.handlerFooterShow(false) // 隐藏底部导航栏
  } else {
    settings.handlerGoBackShow(false) // 隐藏返回按钮
    settings.handlerFooterShow(true) // 展示底部导航栏
  }
  if (!token || !settings.isLogin) {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  } else {
    if (to.path == '/login') {
      next({ path: '/' })
    } else {
      next()
    }
  }
})



export default router